package com.smartstate.dataManagement.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.smartstate.dataManagement.dto.PageDTO;
import com.smartstate.dataManagement.dto.UserDTO;
import com.smartstate.dataManagement.entity.PasswordEntity;
import com.smartstate.dataManagement.entity.Permission;
import com.smartstate.dataManagement.entity.User;
import com.smartstate.dataManagement.service.UserService;
import com.smartstate.dataManagement.util.StringUtils;
import com.smartstate.dataManagement.vo.ResultCode;
import com.smartstate.dataManagement.vo.ResultVO;
import com.smartstate.dataManagement.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;

@RestController
@Api(tags = {"用户控制器"})
@RequestMapping("/user")
@Slf4j
public class UserController {


  @Resource
  private UserService userService;

//  /**
//   * 添加用户和角色信息
//   * @param userDTO
//   * @return
//   */
//  @PostMapping ("/saveOrUpdate")
//  @ApiOperation(value = "新增用户")
//  public ResultVO<Boolean> saveOrUpdate(@RequestBody UserDTO<User> userDTO) {
//    return ResultVO.ok(userService.addUser(userDTO));
//  }

  /**
   * 新建一个用户
   * @param
   * @return
   */
  @PostMapping ("/addNewUser")
  @ApiOperation(value = "新建一个用户")
  public ResultVO<User> addNewUser(@RequestParam("roleId") String roleId,@RequestBody User user) {
    return userService.addNewUser(roleId, user);
  }

  /**
   * 根据用户ID更新用户信息
   * @param
   * @return
//   */
  @PostMapping ("/updateUserById")
  @ApiOperation(value = "根据用户ID更新用户信息")
  public ResultVO<Boolean> updateUserById(@RequestParam String roleId,@RequestBody User user) {
    return userService.updateUserById(roleId,user);
  }


  /**
   * 根据用户ID更新账户状态（1 启用， 0 禁用）
   * @param
   * @return
   */
  @PostMapping ("/updateStatusById")
  @ApiOperation(value = "根据用户ID更新用户账号状态")
  public ResultVO<Boolean> updateStatusById(@RequestParam("id")String id,@RequestParam("state")String state) {
    return userService.updateStatusById(id,state);
  }


  /**
   * 批量删除用户，同时删除用户角色表中的对应信息
   * @param item
   * @return
   */
  @DeleteMapping ("/removeByIds")
  @ApiOperation(value = "根据ID批量删除")
  public ResultVO<Boolean> removeByIds(@RequestBody List<String> item) {
    return userService.removeByIds(item);
  }


  @DeleteMapping ("/delRoleById")
  @ApiOperation(value = "根据ID删除用户")
  @Transactional
  public ResultVO<Boolean> delRoleById(@RequestParam String id) {
   return userService.delUserById(id);
  }


  @PostMapping ("/getById")
  @ApiOperation(value = "根据ID查询")
  public UserVO getById(@RequestParam String id) {
    return userService.getById(id);
  }


  @PostMapping ("/getUserBySpec")
  @ApiOperation(value = "单表分页查询")
  public Page<List<UserVO>> getUserBySpec(@RequestBody PageDTO<User> pageDTO) {
    return userService.queryAllPageByUserDTO(pageDTO);
  }


//  @PostMapping ("/getUserByNameAndStatus")
//  @ApiOperation(value = "单表分页查询")
//  public Page<User> getUserByNameAndStatus(@RequestBody PageDTO<User> pageDTO) {
//    return userService.getUserByNameAndStatus(pageDTO);
//  }



//  @PostMapping ("/getAllUserAndDepartment")
//  @ApiOperation(value = "查询人员及部门")
//  public ResultVO<List<UserVO<User>>> getAllUserAndDepartment(@RequestBody UserDTO<User> item) {
//    return ResultVO.ok(userService.getAllUserAndDepartment(item)) ;
//  }


//  @GetMapping("/getSerialCode")
//  @ApiOperation("获取序列号")
//  public ResultVO<String> getSerialCode(String name){
//    return ResultVO.ok(userService.getSerialCode(name));
//  }



//  @PostMapping("/setRedisValue")
//  @ApiOperation("redis测试")
//  public ResultVO<Boolean> setRedisValue(@RequestParam String value) {
//    return ResultVO.ok(userService.setRedisValue(value));
//  }

//  @GetMapping("/findAll")
//  @ApiOperation(value = "查询所有用户")
//  public ResultVO<List<UserVO<User>>> findAll() {
//    return ResultVO.ok(userService.findAll());
//  }


  @GetMapping("/getList")
  @ApiOperation(value = "查询所有用户")
  public ResultVO<List<User>>  getList() {
    return userService.getList();
  }


  @GetMapping("/getUserByToken")
  @ApiOperation(value = "根据token获取用户信息")
  public  ResultVO<User>  getUserByToken(){
    return  userService.getUserByToken();
  }

  @GetMapping("/getPermsByToken")
  @ApiOperation(value = "根据token获取用户权限信息")
  public  ResultVO<List<Permission>>  getPermsByToken(){
    return  userService.getPermsByToken();
  }



  @PostMapping("/updatePassword")
  @ApiOperation(value = "更新用户的密码")
  public  ResultVO<ResultCode>  updatePassword(@RequestBody PasswordEntity passwordEntity){
    return  userService.updatePassword(passwordEntity);
  }

}
